package com.yanqu.road.server.gameplayer.module.activity.cat;

import com.yanqu.road.entity.activity.cat.*;
import com.yanqu.road.entity.activity.cat.config.CatMissionInfo;
import com.yanqu.road.entity.activity.cat.data.CatUserData;
import com.yanqu.road.pb.activity.CatProto;
import com.yanqu.road.server.gameplayer.module.activity.cat.action.BaseElementAction;
import com.yanqu.road.server.manger.activity.cat.CatMgr;

import java.util.ArrayList;
import java.util.List;

public class ConnectResult {

    boolean end;

    protected int ret;

    protected long damage;

    protected Position rolePosition;

    protected Element[][] elements;

    protected GameData gameData;

    protected List<Position> positionList;

    List<Element> disappearList = new ArrayList<>();

    //万能格子位置
    List<Integer> wanNengList = new ArrayList<>();

    //生成小怪的位置
    List<Integer> newMonsterList = new ArrayList<>();

    //已变化过的元素
    List<Integer> changedList = new ArrayList<>();

    //连线遍历当前下标
    protected int index = -1;

    List<CatProto.CatConnectDetailTemp.Builder> detailBuilderList = new ArrayList<>();

    //盘面遍历当前下标
    protected int allIndex = BaseElementAction.allElementCount;

    //当前已抵消 点数（碰到小怪、牢笼会抵消）
    private int offsetPoint;

    //当前回合配置
    CatMissionInfo missionInfo;

    //最后一个是监牢
    boolean lastPrison;

    //最后一个的类型
    int lastType;

    CatUserData userData;

    List<CatProto.CatGameMoveHistoryTemp.Builder> moveList = new ArrayList<>();

    public ConnectResult(Position rolePosition, List<Position> positionList, Element[][] tempElements, GameData tempStatus, CatMissionInfo missionInfo, CatUserData userData) {
        this.positionList = positionList;
        this.rolePosition = rolePosition;
        this.elements = tempElements;
        this.gameData = tempStatus;
        this.missionInfo = missionInfo;
        this.userData = userData;
        check();
    }

    private void check() {
        int size = positionList.size();
        Position position = positionList.get(size - 1);
        Element element = elements[position.getX()][position.getY()];
        if(CatMgr.hasAttr(element, eElementAttrTypes.Prison.getType()) != null){
            lastPrison = true;
        }
        lastType = element.getType();
    }

    /**
     * 获取总攻击点数
     */
    public int getAttackPoint(){
        int size = positionList.size();
        Position position = positionList.get(size - 1);
        //策划又不要特殊情况了，全部连线格子都算攻击力
        //Element element = elements[position.getX()][position.getY()];
        //if(lastType == eElementTypes.Monster.getType() || lastPrison){
        //    return size - 1;
        //}
        return size;
    }

    /**
     * 获取主角最后的位置
     */
    public Position getRoleLastPosition(){
        int size = positionList.size();
        Position position = positionList.get(size - 1);
        CatMgr.getLogger().info("getRoleLastPosition : {}", lastPrison);
        if(lastPrison){
            return positionList.get(size - 2);
        }
        return position;
    }

    /**
     * 获取剩余攻击力
     */
    public int getCurAttackPoint(){
        return getAttackPoint() - offsetPoint;
    }

    /**
     * 获取当前路径位置
     */
    public int getPositionIndex(int x, int y){
        for (int i = 0; i < positionList.size(); i++) {
            Position position = positionList.get(i);
            if(x == position.getX() && y == position.getY()){
                return i;
            }
        }
        return -1;
    }

    public boolean isEnd() {
        return end;
    }

    public void setEnd(boolean end) {
        this.end = end;
    }

    public int getRet() {
        return ret;
    }

    public void setRet(int ret) {
        this.ret = ret;
        if(ret == -1){
           StackTraceElement[] stackTraceElements = Thread.getAllStackTraces().get(Thread.currentThread());
           if(stackTraceElements != null){
               for (StackTraceElement traceElement : stackTraceElements) {
                   CatMgr.getLogger().info("ConnectResult -- " + traceElement);
               }
           }
        }
    }

    public long getDamage() {
        return damage;
    }

    public void setDamage(long damage) {
        this.damage = damage;
    }

    public List<Position> getPositionList() {
        return positionList;
    }

    public void setPositionList(List<Position> positionList) {
        this.positionList = positionList;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Position getRolePosition() {
        return rolePosition;
    }

    public void setRolePosition(Position rolePosition) {
        this.rolePosition = rolePosition;
    }

    public Element[][] getElements() {
        return elements;
    }

    public void setElements(Element[][] elements) {
        this.elements = elements;
    }

    public GameData getGameData() {
        return gameData;
    }

    public void setGameData(GameData gameData) {
        this.gameData = gameData;
    }

    public int getAllIndex() {
        return allIndex;
    }

    public void setAllIndex(int allIndex) {
        this.allIndex = allIndex;
    }

    public List<Element> getDisappearList() {
        return disappearList;
    }

    public void setDisappearList(List<Element> disappearList) {
        this.disappearList = disappearList;
    }

    public List<Integer> getWanNengList() {
        return wanNengList;
    }

    public void setWanNengList(List<Integer> wanNengList) {
        this.wanNengList = wanNengList;
    }

    public List<Integer> getChangedList() {
        return changedList;
    }

    public void setChangedList(List<Integer> changedList) {
        this.changedList = changedList;
    }

    public List<CatProto.CatConnectDetailTemp.Builder> getDetailBuilderList() {
        return detailBuilderList;
    }

    public List<Integer> getNewMonsterList() {
        return newMonsterList;
    }

    public void setNewMonsterList(List<Integer> newMonsterList) {
        this.newMonsterList = newMonsterList;
    }

    public void setDetailBuilderList(List<CatProto.CatConnectDetailTemp.Builder> detailBuilderList) {
        this.detailBuilderList = detailBuilderList;
    }

    public int getOffsetPoint() {
        return offsetPoint;
    }

    public void setOffsetPoint(int offsetPoint) {
        this.offsetPoint = offsetPoint;
    }

    public CatMissionInfo getMissionInfo() {
        return missionInfo;
    }

    public void setMissionInfo(CatMissionInfo missionInfo) {
        this.missionInfo = missionInfo;
    }

    public List<CatProto.CatGameMoveHistoryTemp.Builder> getMoveList() {
        return moveList;
    }

    public CatUserData getUserData() {
        return userData;
    }
}
